﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Comm;

public partial class Web_Admin_User_user_list : AdminPageBase
{
    private BLL.User bll = new BLL.User();
    protected void Page_Load(object sender, EventArgs e)
    {
        base.CheckPopedomInfoRedirect(1010, 1);
        but_pass.Enabled = but_cancelpass.Enabled = base.CheckPopedomInfo(1010, 16);
        but_delete.Enabled = base.CheckPopedomInfo(1010, 8);
        if (!IsPostBack)
        {
            loadType();
            loadData();
        }   
    }

    /// <summary>
    /// 加载类型
    /// </summary>
    private void loadType()
    {
        //typelist = typebll.GetModelList("");
    }

    /// <summary>
    /// 获取条件
    /// </summary>
    /// <returns></returns>
    private string getStrWhere()
    {
        string strWhere = "";
        string username = base.QueryString("txt_UserName");
        if (!string.IsNullOrEmpty(username))
        {
            strWhere += " UserName like '%" + username + "%'";
            txt_UserName.Text = base.PrototypeQuery("txt_UserName");
        }
        string usertype = base.QueryString("drpUserType");
        if (!string.IsNullOrEmpty(usertype))
        {
            strWhere += " and UserType=" + usertype;
            drpUserType.SelectedValue = usertype;
        }
        strWhere = System.Text.RegularExpressions.Regex.Replace(strWhere, "^ and", "");

        return strWhere;
    }

    /// <summary>
    /// 获取排序
    /// </summary>
    /// <returns></returns>
    private string getSort()
    {
        string sort = "AddTime desc";
        string sortid = base.QueryString("drpSort");
        if (string.IsNullOrEmpty(sortid))
        {
            return sort;
        }

        switch (sortid)
        {
            case "1": sort = "AddTime asc"; break;
            case "2": sort = "Integral desc"; break;
            case "3": sort = "Integral asc"; break;
        }
        drpSort.SelectedValue = sortid;

        return sort;
    }

    /// <summary>
    /// 加载数据
    /// </summary>
    private void loadData()
    {
        Model.PageData data = bll.GetList(AppClass.PageSize, base.PageIndex, getStrWhere(), getSort());
        rpList.DataSource = data.DataSoure;
        rpList.DataBind();
        pgServer.RecordCount = data.Count;
        pgServer.PageSize = AppClass.PageSize;
        pgServer.DataBind();
        lit_nodata.Visible = data.Count < 1 ? true : false;
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void but_delete_Click(object sender, EventArgs e)
    {
        string ids = Request.Form["checkboxs"];
        if (string.IsNullOrEmpty(ids))
        {
            return;
        }

        bll.DeleteList(ids);
        base.AddLog(3, "会员管理ID：" + ids);
        loadData();
    }

    /// <summary>
    /// 审核
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void but_pass_Click(object sender, EventArgs e)
    {
        string ids = Request.Form["checkboxs"];
        if (string.IsNullOrEmpty(ids))
        {
            return;
        }
        Model.User model = new Model.User();
        model.IsPass = true;
        bll.Update(model, "ID in (" + ids + ")");
        base.AddLog(2, "通过审核会员管理ID：" + ids);
        loadType();
        loadData();
    }

    /// <summary>
    /// 取消审核
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void but_cancelpass_Click(object sender, EventArgs e)
    {
        string ids = Request.Form["checkboxs"];
        if (string.IsNullOrEmpty(ids))
        {
            return;
        }

        Model.User model = new Model.User();
        model.IsPass = false;
        bll.Update(model, "ID in (" + ids + ")");
        base.AddLog(2, "取消审核会员管理ID：" + ids);
        loadType();
        loadData();
    }

    /// <summary>
    /// 导出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void but_excel_Click(object sender, EventArgs e)
    {
        string[] fileName = { "ID", "UserName", "UserPassword", "Question", "Answer", "UserType", "Integral", "IsPass", "HeahImagePath", "Pic", "IsPass", "IsTop", "Hits", "AddTime" };
        string[] title = { "ID", "用户名", "密码", "密码保护问题", "答案", "会员类型", "积分", "审核", "昵称", "图片", "审核", "置顶", "浏览数", "添加时间" };
        DataToExcel.Export(fileName, title, "User", "会员列表_" + DateTime.Now.ToString("yyy-MM-dd"), getStrWhere(), getSort());
    }
}
